package sort;

/**
 * 选择排序算法实现：时间复杂度是O（N^2）,空间复杂度是O（1）
 *
 * @author sjm
 * @creed: Wellerman
 * @date 2022/3/3 12:03
 */
public class SelectSort {

    public static void select(int[] arrs) {
        //遍历
        for (int i = 0; i < arrs.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arrs.length; j++) {
                //判断
                minIndex = arrs[minIndex] <= arrs[j] ? minIndex : j;
            }
            //交换
            swap(arrs, i, minIndex);
        }
    }

    private static void swap(int[] arrs, int i, int minIndex) {
        int temp = arrs[i];
        arrs[i] = arrs[minIndex];
        arrs[minIndex] = temp;

    }

    public static void main(String[] args) {

        int[] arrs = {8, 5, 23, 1, 5};
        select(arrs);
        for (int i = 0; i < arrs.length; i++) {
            System.out.println(arrs[i]);
        }
    }
}
